package com.ruochen.recipe.customer;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.ruochen.common.adapter.AbsCommonAdapter;
import com.ruochen.common.adapter.AbsViewHolder;
import com.ruochen.common.adapter.RecyclerCommonAdapter;
import com.ruochen.common.adapter.RecyclerViewHolder;
import com.ruochen.common.listener.NoDoubleClickListener;
import com.ruochen.common.widget.NonScrollRecyclerView;
import com.ruochen.common.widget.NonSlidingGridView;
import com.ruochen.recipe.R;
import com.ruochen.recipe.entity.CookDishEntity;
import com.ruochen.recipe.entity.CookbookEntity;
import com.ruochen.recipe.ui.activity.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class ImproveInformationPopup extends CenterPopupView {

    private TextView rtvSure;
    private TextView rtvCancel;
    private NonScrollRecyclerView nsrvMenuExpandable;

    public ImproveInformationPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        rtvSure = findViewById(R.id.rtvSure);
        rtvCancel = findViewById(R.id.rtvCancel);
        nsrvMenuExpandable = findViewById(R.id.nsrvMenuExpandable);
        rtvSure.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                dismissWith(new Runnable() {
                    @Override
                    public void run() {
                        ActivityUtils.startActivity(new Intent(getContext(), MainActivity.class));
                    }
                });
            }
        });
        rtvCancel.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                dismiss();
            }
        });
        List<CookbookEntity> list = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            final List<CookDishEntity> childList = new ArrayList<>(i);
            for (int j = 0; j < i; j++) {
                childList.add(new CookDishEntity("菜肴 " + j));
            }
            list.add(new CookbookEntity("菜谱 " + i, childList));
        }

        nsrvMenuExpandable.setLayoutManager(new LinearLayoutManager(getContext()));
        nsrvMenuExpandable.setAdapter(new RecyclerCommonAdapter<CookbookEntity>(getContext(), R.layout.item_cookbook_group, list) {
            @Override
            protected void convert(RecyclerViewHolder holder, CookbookEntity item, int position) {
                holder.setText(R.id.rtvItemName, item.getName());
                NonSlidingGridView nsgvItemDishGrid = holder.getView(R.id.nsgvItemDishGrid);
                if (item.getChildList() != null && item.getChildList().size() > 0) {
                    nsgvItemDishGrid.setVisibility(VISIBLE);
                    nsgvItemDishGrid.setAdapter(new AbsCommonAdapter<CookDishEntity>(getContext(), R.layout.item_cookbook_child, item.getChildList()) {
                        @Override
                        public void convert(AbsViewHolder childHolder, CookDishEntity item, int position) {

                        }
                    });
                    nsgvItemDishGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            showChooseDish();
                        }
                    });
                } else {
                    nsgvItemDishGrid.setVisibility(GONE);
                }
            }
        });
    }

    private void showChooseDish() {
        new XPopup.Builder(getContext())
                .dismissOnTouchOutside(false)
                .dismissOnTouchOutside(false)
                .enableDrag(false)
                .autoOpenSoftInput(false)
                .autoFocusEditText(false)
                .moveUpToKeyboard(false)
                .popupWidth(SizeUtils.dp2px(540))
                .isDestroyOnDismiss(true)
                .asCustom(new CookDishEditPopup(getContext()))
                .show();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_improve_information;
    }

    @Override
    protected int getMaxWidth() {
        return (int) (ScreenUtils.getScreenWidth() * .8f);
    }

    @Override
    protected int getMaxHeight() {
        return (int) (ScreenUtils.getScreenHeight() * .8f);
    }
}
